/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

import java.util.List;
import java.util.Random;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * <p>Project: javase - IntStreamDemo
 * <p>Powered by webrx On 2021-12-21 16:53:52
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class IntStreamDemo {
    public static void main(String[] args) {
        //计算1+2+3+...+100 = ？sum
        int sum = 0;
        for (int i = 1; i <= 100; i++) {
            sum += i;
        }
        System.out.println(sum);

        int ss = IntStream.rangeClosed(1, 100).sum();
        System.out.println(ss);
        //Random rand = new Random();
        //生成5个随机整数（1-100),并输出
        //IntStream.generate(() -> new Random().nextInt(100) + 1).limit(5).forEach(System.out::println);
        //生成15个随机整数，并过滤一下偶数，并输出
        //IntStream.generate(() -> new Random().nextInt(100) + 1).limit(15).filter(a -> a % 2 == 0).forEach(System.out::println);
        IntStream
                .generate(() -> new Random()
                        .nextInt(100) + 1)
                .limit(15).filter(a -> a % 2 == 0)
                .forEach(a -> {
                    System.out.printf("ok-num:%02d%n", a);
                });

        List<String> list = Stream.generate(() -> {
            Random rand = new Random();
            StringBuilder sbu = new StringBuilder();
            for (int i = 0; i < rand.nextInt(10) + 3; i++) {
                String letter = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
                sbu.append(letter.charAt(rand.nextInt(letter.length())));
            }
            return sbu.toString();
        }).limit(15).toList();
        System.out.println(list);
    }
}
